package com.server.user;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class UserUtil {

	@Autowired
	UserDAO userDao;
	
	

	/**
	 * 判断Openid2user表是否有对应关系，如果没有则插入，如果有，则更新
	 * @param userid
	 * @param openid
	 */
	public void InsOpenid2user(String userid,String openid)
	{
		Map<String, Object> user = userDao.getOpenid2user(openid);
		if(user==null){
			userDao.insOpenid2user(userid, openid);
		}else{
			if(!user.get("userid").equals(userid)){
				userDao.upOpenid2user(userid, openid);
			}
		}
	}
	
	/**
	 * 获取下一个8位推荐码
	 * @return
	 */
	public String getTjcode()
	{
		String res = "";
		Map<String, Object> map_res = userDao.getSeq();
		int seq=(Integer)map_res.get("val");
		//补位
		if(seq<10)
		{
			res="0000000"+seq;
		}else if(seq<100)
		{
			res="000000"+seq;
		}else if(seq<1000)
		{
			res="00000"+seq;
		}else if(seq<10000)
		{
			res="0000"+seq;
		}else if(seq<100000)
		{
			res="000"+seq;
		}else if(seq<1000000)
		{
			res="00"+seq;
		}else if(seq<10000000)
		{
			res="0"+seq;
		}else{
			res = ""+seq;
		}
		
		
		return "GS"+res;
	}
}
